/**
 * <pre>
 * Title: 		ArticleGroupServiceImpl.java
 * Project: 	MSOSS
 * Author:		pengfangliang
 * Create:	 	2012-8-7 上午10:24:30
 * Copyright: 	Copyright (c) 2012
 * Company:		Shenzhen Helper
 * <pre>
 */
package com.hengpeng.itfinoss.service.article.impl;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.persist.query.Projections;
import com.hengpeng.common.persist.query.model.Order.OrderType;
import com.hengpeng.common.util.StringHelper;
import com.hengpeng.itfinbase.en.ArticleGroupType;
import com.hengpeng.itfinbase.persist.Article;
import com.hengpeng.itfinbase.persist.ArticleGroup;
import com.hengpeng.itfinbase.persist.ArticleGroupEx;
import com.hengpeng.itfinbase.persist.UserInfo;
import com.hengpeng.itfinoss.service.ServiceFactory;
import com.hengpeng.itfinoss.service.article.ArticleGroupSerivce;

/**
 * <pre>
 * 文章分组接口实现
 * </pre>
 * 
 * @author pengfangliang
 * @version 1.0, 2012-8-7
 */
@Service
@Lazy
public class ArticleGroupServiceImpl implements ArticleGroupSerivce {
    @Autowired
    private ServiceFactory serviceFactory;

    @Override
    public ServiceResult<ArticleGroup> delete(Set<Object> ids) {
        for (Object id : ids) {
            serviceFactory.getPersistService().delete(ArticleGroup.class, id);
        }
        return new ServiceResult<ArticleGroup>();
    }

    @Override
    public ServiceResult<List<ArticleGroup>> findAll() {
        List<ArticleGroup> list = serviceFactory.getPersistService().list(ArticleGroup.class);
        return new ServiceResult<List<ArticleGroup>>(list);
    }

    @Override
    public ServiceResult<Long> findArticleGroupCount(String name) {
        DynamicQuery pagination = new DynamicQuery(ArticleGroup.class);
        if (!StringHelper.isEmpty(name))
            pagination.like("name", "%" + name + "%");
        return new ServiceResult<Long>(serviceFactory.getPersistService().findPageCount(pagination, null));
    }

    @Override
    public ServiceResult<List<ArticleGroup>> findArticleGroupPage(String name, String remark, Integer pageIndex,
            Integer pageSize, LinkedHashMap<String, OrderType> sortMap) {
        DynamicQuery pagination = new DynamicQuery(ArticleGroup.class);
        if (!StringHelper.isEmpty(name))
            pagination.like("name", "%" + name + "%");
        if (!StringHelper.isEmpty(remark))
            pagination.like("remark", "%" + remark + "%");
        pagination.setPageIndex(pageIndex);
        pagination.setPageSize(pageSize);
        if (sortMap != null) {
            for (Entry<String, OrderType> entry : sortMap.entrySet()) {
                if (entry.getValue() == OrderType.DESC) {
                    pagination.desc(entry.getKey());
                } else {
                    pagination.asc(entry.getKey());
                }
            }
        }
        return new ServiceResult<List<ArticleGroup>>(serviceFactory.getPersistService().find(ArticleGroup.class,
                pagination));
    }

    @Override
    public ServiceResult<ArticleGroup> get(Long id) {
        ArticleGroup articleGroup = serviceFactory.getPersistService().get(ArticleGroup.class, id);
        return new ServiceResult<ArticleGroup>(articleGroup);
    }

    @Override
    public ServiceResult<ArticleGroup> save(ArticleGroup articleGroup) {
        ArticleGroup obj = serviceFactory.getPersistService().save(articleGroup);
        return new ServiceResult<ArticleGroup>(obj);
    }

    @Override
    public ServiceResult<ArticleGroup> update(ArticleGroup articleGroup) {
        ArticleGroup obj = serviceFactory.getPersistService().update(articleGroup);
        return new ServiceResult<ArticleGroup>(obj);
    }

    @Override
    public Long countArticleGroupEx(Long[] groupIds, Long[] articleIds) {
        DynamicQuery dynamicQuery = new DynamicQuery(ArticleGroupEx.class);
        dynamicQuery.inArray("groupId", groupIds);
        dynamicQuery.inArray("articleId", articleIds);
        return serviceFactory.getPersistService().findPageCount(dynamicQuery, null);
    }

    @Override
    public List<ArticleGroupEx> findArticleGroupEx(Long[] groupIds, Long[] articleIds) {
        DynamicQuery dynamicQuery = new DynamicQuery(ArticleGroupEx.class);
        dynamicQuery.inArray("groupId", groupIds);
        dynamicQuery.inArray("articleId", articleIds);
        dynamicQuery.asc("articleId");
        return serviceFactory.getPersistService().find(ArticleGroupEx.class, dynamicQuery);
    }

    /**
     * 根据类型查询新闻分组
     */
    @Override
    public ArticleGroup getArticleByGroupType(ArticleGroupType type) {
        DynamicQuery query = new DynamicQuery(ArticleGroup.class);
        query.eq("articleGroupType", type);
        List<ArticleGroup> list = serviceFactory.getPersistService().find(ArticleGroup.class, query);
        if (list != null && list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    }
}
